www.gusucode.com > VC++ 实现多元线性回归-源码程序 > VC++ 实现多元线性回归-源码程序/code/VC实现多元线性回归/AGE检测仪View.cpp

    //Download by http://www.NewXing.com
// AGE检测仪View.cpp : implementation of the CAGEView class
//

#include "stdafx.h"
#include "AGE检测仪.h"

#include "AGE检测仪Doc.h"
#include "AGE检测仪View.h"
#include <vector>
#include <iostream>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAGEView

IMPLEMENT_DYNCREATE(CAGEView, CView)

BEGIN_MESSAGE_MAP(CAGEView, CView)
	//{{AFX_MSG_MAP(CAGEView)
	ON_COMMAND(IDM_REGRESS, OnRegress)
	ON_COMMAND(IDM_DWT, OnDwt)
	ON_COMMAND(IDM_INTRODUCTION, OnIntroduction)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAGEView construction/destruction

CAGEView::CAGEView()
{
	// TODO: add construction code here
	n = 10;
	equation = "";
}

CAGEView::~CAGEView()
{
}

BOOL CAGEView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CAGEView drawing

void CAGEView::OnDraw(CDC* pDC)
{
	CAGEDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CAGEView printing

BOOL CAGEView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CAGEView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CAGEView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CAGEView diagnostics

#ifdef _DEBUG
void CAGEView::AssertValid() const
{
	CView::AssertValid();
}

void CAGEView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CAGEDoc* CAGEView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAGEDoc)));
	return (CAGEDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAGEView message handlers

void CAGEView::OnRegress() 
{
	// TODO: Add your command handler code here
	int i;
	static double a11,a12,a13,a21,a22,a23,a31,a32,a33,c1,c2,c3;
	double A,B,C;
	for(i=1;i<=n;i++)           //输入 points1[i].y,points1[i].x1与points2[i].x2
	{                            
		a12+=points1[i].x1;
		a13+=points2[i].x2;
		a22+=points1[i].x1*points1[i].x1;
		a23+=points1[i].x1*points2[i].x2;
		a33+=points2[i].x2*points2[i].x2;
		c1+=points1[i].y;
		c2+=points1[i].x1*points1[i].y;
		c3+=points2[i].x2*points1[i].y;
	}
	a11=n; 
	a21=a12;
	a31=a13;
	a32=a23;                     //求解 
	A=c1*(a22*a33-a23*a32)/(a11*a22*a33-a11*a23*a32-a21*a12*a33+a21*a13*a32+a31*a12*a23-a31*a13*a22)-c2*(a12*a33-a13*a32)/(a11*a22*a33-a11*a23*a32-a21*a12*a33+a21*a13*a32+a31*a12*a23-a31*a13*a22)+c3*(a12*a23-a13*a22)/(a11*a22*a33-a11*a23*a32-a21*a12*a33+a21*a13*a32+a31*a12*a23-a31*a13*a22); 
	B=-c1*(a21*a33-a23*a31)/(a11*a22*a33-a11*a23*a32-a21*a12*a33+a21*a13*a32+a31*a12*a23-a31*a13*a22)+c2*(a11*a33-a13*a31)/(a11*a22*a33-a11*a23*a32-a21*a12*a33+a21*a13*a32+a31*a12*a23-a31*a13*a22)-c3*(a11*a23-a13*a21)/(a11*a22*a33-a11*a23*a32-a21*a12*a33+a21*a13*a32+a31*a12*a23-a31*a13*a22); 
	C=c1*(a21*a32-a22*a31)/(a11*a22*a33-a11*a23*a32-a21*a12*a33+a21*a13*a32+a31*a12*a23-a31*a13*a22)-c2*(a11*a32-a12*a31)/(a11*a22*a33-a11*a23*a32-a21*a12*a33+a21*a13*a32+a31*a12*a23-a31*a13*a22)+c3*(a11*a22-a12*a21)/(a11*a22*a33-a11*a23*a32-a21*a12*a33+a21*a13*a32+a31*a12*a23-a31*a13*a22);
//	cout<<"所得拟合函数为;"<<endl;
//	::cout<<"p(x)="<<A<<"+("<<B<<")*x1+("<<C<<")*x2";       //输出
//	equation = "y="+A;
	CString str;
	str.Format("y= %lf + (%lf)*X1 + (%lf)*X2",A,B,C);
	CClientDC dc(this);
	dc.TextOut(0,0,str);
/*	dc.MoveTo(100,100);
	for(int j=1;j<9;j++)
	{		
		dc.LineTo(100+j*200,100+j*B);
		dc.MoveTo(100+(j+1)*200,100+(j+1)*B);
	}*/
}

void CAGEView::OnDwt() 
{
	// TODO: Add your command handler code here
	
}

void CAGEView::OnIntroduction() 
{
	// TODO: Add your command handler code here
	MessageBox("首先打开您要处理的数据,然后在数据处理菜单下点击您要选择的处理方法");
}

void CAGEView::OnFileOpen() 
{
	// TODO: Add your command handler code here
	CFileDialog fileDlg(TRUE);
	fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
	if(IDOK==fileDlg.DoModal())
	{
		CStdioFile   file(fopen( fileDlg.GetFileName(), "r" ));  
		//	file.Open(fileDlg.GetFileName(),CFile::modeRead);
			//file.Open( pFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeText )
			//int   i   =   0   ;
	//	float a=0,b=0,c=0;
	//	point1 points1[10];
     //   point2 points2[10];
		CString  str;
		int i=0;
		//	file.ReadString(str);
		while(file.ReadString(str))   
			{ 
				sscanf(str,"%f %f %f", &(points1[i].y),&(points1[i].x1),&(points2[i].x2)); 
				i++;//	  i++   ;
			}
		file.Close();
				/*
			CFile file(fileDlg.GetFileName(),CFile::modeRead);
		float *pBuf;
		DWORD dwFileLen;
		dwFileLen=file.GetLength();
		pBuf=new float[dwFileLen+1];
		pBuf[dwFileLen]=0;
    	file.Read(pBuf,dwFileLen);
    	float a = pBuf[4];
		file.Close();
		CFile file1("99.txt",CFile::modeCreate|CFile::modeWrite );
		file1.Write(&a,4);
			file1.Close();
		FILE   file   =   fopen(fileDlg.GetFileName(),"w+" ); 
		float     f   ;
		vector<float>   af;   
		while(fscanf(file,   "%f",   &f)==1)   
		{   
			af.push_back(f);   
		}   */
	}
}
void CAGEView::OnFileSaveAs() 
{
	// TODO: Add your command handler code here
	
}